package com.d0319.Z19;

public class Money  {
    int money=1000;
    public void Guitai(){
          while(money>0) {
              synchronized (this){
                  Thread.currentThread().setName("A");
                  System.out.print(Thread.currentThread().getName() + "在柜台取走" + 100 + "元" + " ");
                  money = money - 100;
                  System.out.println("剩余钱数：" + money + "元");
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  if(money==0){
                      System.out.println("钱取完了");
                      System.exit(0);
                  }
              }
          }

    }
    public void ATM(){
        while(money>0){
            synchronized (this){
                Thread.currentThread().setName("B");
                System.out.print(Thread.currentThread().getName()+"在ATM取走"+100+"元"+" ");
                money=money-100;
                System.out.println("剩余钱数："+money+"元");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(money==0){
                    System.out.println("钱取完了");
                    System.exit(0);
                }
            }
        }

    }
    public static void main(String[] args) {
        Money mon=new Money();
        new Thread(()->mon.Guitai()).start();
        new Thread(()->mon.ATM()).start();

    }
}
